//
//  DevelopmentViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/29.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "DevelopmentViewController.h"
#import "SelectImageView.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import <WebKit/WebKit.h>
@interface DevelopmentViewController ()<TZImagePickerControllerDelegate>

@property (nonatomic, strong)WKWebView *webView;
@property (weak, nonatomic) IBOutlet SelectImageView *selectImageView;
@property (weak, nonatomic) IBOutlet UIView *webBackView;

@property (weak, nonatomic) IBOutlet UIView *backView;
@property (nonatomic, strong) NSArray *photos;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic, strong) YMTextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *phoneBtn;
@end

@implementation DevelopmentViewController

-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}
-(NSMutableArray *)nameArray{
    if (!_nameArray) {
        _nameArray = [NSMutableArray array];
    }
    return _nameArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发展建议";
    [self.phoneBtn setTitle:[TokenManager getHotTel] forState:UIControlStateNormal];
    self.textView=[[YMTextView alloc]init];
    [self.backView addSubview:self.textView];
    self.textView.maxNumb = 200;
    self.textView.placeholderColor = [UIColor lightGrayColor];
    self.textView.textFont = [UIFont systemFontOfSize:15];
    self.textView.backgroundColor=[UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    self.textView.placeholder = @" 欢迎您提出宝贵的意见和建议";
    self.textView.isFinishKeyboard=YES;
    
    self.webView = [[WKWebView alloc] init];
//          NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
//
//                   WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//                   WKUserContentController *wkUController = [[WKUserContentController alloc] init];
//                   [wkUController addUserScript:wkUScript];
//
//                   WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
//                   wkWebConfig.userContentController = wkUController;
//
//                   // 创建设置对象
//                   WKPreferences *preference = [[WKPreferences alloc]init];
//                   // 设置字体大小(最小的字体大小)
//                   preference.minimumFontSize = 10;
//                   // 设置偏好设置对象
//                   wkWebConfig.preferences = preference;
//                   self.webView= [[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebConfig];
//          
          
          
            [self.webBackView addSubview:self.webView];

            UIEdgeInsets padding = UIEdgeInsetsMake(0, 0, 0, 0);
            [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.webBackView).with.insets(padding);
            }];
    

    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self getdata];
}

-(void)getdata{
    
    [[ApiManager ShardInstance]getDetailNoticeID:@"60" resultBlock:^(NSDictionary *data, NSError *error) {
           BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
           if (model.code == 1) {
            
                [self.webView loadHTMLString:[NSString stringWithFormat:@"%@",data[@"rs"][@"content"]] baseURL:nil];
           }
           
       }];
}

#pragma mark - Table view data source

- (IBAction)selectImageBtn:(UIButton *)sender {
    
    if(self.photos.count >=4){
           UIAlertView *aler  = [[UIAlertView alloc]initWithTitle:@"" message:@"最多可以选择4张哦" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
           [aler show];
       }
       TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:4 delegate:self];
       
       // 你可以通过block或者代理，来得到用户选择的照片.
    WS(weakSelf);
       [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets,BOOL isSelectOriginalPhoto) {
           weakSelf.selectImageView.images = photos;
           weakSelf.photos = photos;
           __block NSMutableArray *tempArray = photos;
           weakSelf.selectImageView.deleteItem = ^(NSInteger index) {
               [tempArray removeObjectAtIndex:index];
               weakSelf.selectImageView.images = tempArray;
               weakSelf.photos = tempArray;
               
               
              
           };
//           weakSelf.selectImageView.selectImage = ^(NSInteger index) {
//
//              [self selectedIndex:index withImageURLs:weakSelf.photos];
//
//           };
           [[ApiManager ShardInstance]upLoadImageimageArray:weakSelf.photos resultBlock:^(NSDictionary *data, NSError *error) {
               [weakSelf.imageArray addObjectsFromArray:data[@"rs"]];
           }];
       }];
    
       [self presentViewController:imagePickerVc animated:YES completion:nil];
}



- (IBAction)commitBtn:(BoardButton *)sender {
    
    for (int i =0; i< self.imageArray.count; i++) {
        
        [self.nameArray addObject:[NSString stringWithFormat:@"ios%d",i]];
        
    }
    WS(weakSelf);
    [[ApiManager ShardInstance]getInfoPoststaff_id:[TokenManager getUserId] type:@"1" content:self.textView.textView.text name:@"" work_address:@"" photo_name:self.nameArray photo_urls:self.imageArray resultBlock:^(NSDictionary *data, NSError *error) {
        BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
        if (model.code == 1) {
            [[ToastManager shardInstance]showToast:@"提交成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];

}

- (IBAction)phoneBtn:(UIButton *)sender {
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",[TokenManager getHotTel]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}


- (void)selectedIndex:(NSInteger)index withImageURLs:(NSArray *)urlsArray{
    NSMutableArray *datas = [NSMutableArray array];
    [urlsArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 网络图片
        YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:obj];
        //        data.projectiveView = [self viewAtIndex:idx];
        [datas addObject:data];
    }];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = index;
    [browser show];
}


@end
